//
//  DMoreViewController.m
//  dance
//
//  Created by aether on 2023/10/20.
//

#import "DMoreViewController.h"
#import "DFeedbackViewController.h"

@interface DMoreViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong) IBOutlet UIView *bg;
@property (nonatomic, strong) IBOutlet UITableView *table;

@end

@implementation DMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initValues];
//    [self initViews];
}

- (void) initValues {
    self.items = @[@[@"用户反馈", @"清理缓存"], @[@"崩溃报告", @"检查更新"]];
}

- (void) initViews {
    self.title = @"更多";
    [self.table setBackgroundColor:[UIColor clearColor]];
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame) + 20, CGRectGetHeight(self.view.frame));
    gl.startPoint = CGPointMake(0.5, 0);
    gl.endPoint = CGPointMake(0.5, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:71/255.0 green:0/255.0 blue:233/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:51/255.0 green:149/255.0 blue:247/255.0 alpha:0.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
    [self.bg.layer addSublayer:gl];
}

- (void) viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self initViews];
}

#pragma mark - 代理事件

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.items count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.items objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

//单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell"; // 单元格标识符
    
    // 从重用队列中获取单元格，如果没有可重用的，则创建新的单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    // 设置单元格的文本
    cell.textLabel.text = [[self.items objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
//    cell.textLabel.text = @"关闭";
    
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        DFeedbackViewController *dinfo = [[DFeedbackViewController alloc] initWithNibName:@"DFeedbackViewController" bundle:nil];
        [self.navigationController pushViewController:dinfo animated:YES];
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        [self clearCache];
    } else if (indexPath.section == 1 && indexPath.row == 0) {
        [self crashReport];
    } else if (indexPath.section == 1 && indexPath.row == 1) {
        [self checkUpdate];
    }
}

#pragma mark - 内部事件

- (void) clearCache {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清理" message:[NSString stringWithFormat:@"清理缓存 %d M", arc4random_uniform(61) + 20] preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 处理确定按钮的操作
    }];

    [alertController addAction:okAction];

    // 在视图控制器中展示UIAlert
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void) crashReport {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清理" message:@"暂未发生崩溃" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 处理确定按钮的操作
    }];

    [alertController addAction:okAction];

    // 在视图控制器中展示UIAlert
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void) checkUpdate {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"已升级到最新版本" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"提示" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 处理确定按钮的操作
    }];

    [alertController addAction:okAction];

    // 在视图控制器中展示UIAlert
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
